package cn.ekuma.pos.pda.inventory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import cn.ekuma.android.ui.filter.RefSelectFilter;
import cn.ekuma.android.ui.filter.RefSelectFilter.RefSelectedListener;
import cn.ekuma.data.ui.android.AbstractBaseBeanListAdapter;
import cn.ekuma.data.ui.android.AbstractFinderActivate;
import cn.ekuma.data.ui.android.AbstractViewBeanDetailActivate;
import cn.ekuma.data.ui.android.AbstractViewListenerImplActivate;
import cn.ekuma.epos.android.adapter.CustomerListAdapter;
import cn.ekuma.epos.android.finder.CustomerCategoryFinderActivate;
import cn.ekuma.epos.datalogic.DataLogicPDA;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.db.table.I_Customer;

import com.openbravo.bean.Customer;
import com.openbravo.bean.CustomerCategory;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;

public class CustomerViewActivate extends AbstractViewListenerImplActivate<Customer>{

	
	public static final int SELECT_PRODUCTCATEGORY=3;

	private RefSelectFilter advFilter;
	
	private CustomerCategory customerCategory;
	
	public View createAdvFilterPanel() {
		 advFilter=new RefSelectFilter(this,SELECT_PRODUCTCATEGORY,"分类");
		 advFilter.setRefSelectedListener(new RefSelectedListener(){
			
			public void refSelected(RefSelectFilter filter, int requestCode) {
				Intent intent = new Intent();
				intent.setClass(CustomerViewActivate.this,CustomerCategoryFinderActivate.class);
				startActivityForResult(intent, requestCode);
			} 
		 });
		return advFilter;
	}

	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if(resultCode==RESULT_OK){
			switch(requestCode){
	    	   case SELECT_PRODUCTCATEGORY:
	    		   customerCategory=(CustomerCategory) data.getSerializableExtra(AbstractFinderActivate.SELECTED_BEAN);
	    		   advFilter.setSelectedBeanText(customerCategory.getName());
	    		   break;
	    	}
		}else{
			switch(requestCode){
			case SELECT_PRODUCTCATEGORY:
	    		   customerCategory=null;
	    		   advFilter.setSelectedBeanText(null);
	    		   break;
	    	}
		}
	}

	
	public void baseBeanSelected(Customer obj) {
		Intent intent = new Intent();
    	intent.setClass(CustomerViewActivate.this, CustomerDetailViewActivate.class);
    	Bundle mBundle=new Bundle();
    	mBundle.putSerializable(AbstractViewBeanDetailActivate.SELECTED_BEAN, obj);
    	intent.putExtras(mBundle);
    	startActivity(intent);
	}

	
	public void timeCycleChanged(Date startdate, Date enddate) {
		Object[] afilter=(Object[])createValue();
		afilter[0] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
        afilter[1] = startdate;
        afilter[2] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
        afilter[3] = enddate;
        query(afilter);
	}
	 
	
	public void filterParaReset() {
		customerCategory=null;
		advFilter.setSelectedBeanText(null);
		quickSeachText.setText(null);
		quickTimeFilter.resertValues();
	}

	
	public void initAdvSeachView() {
		// TODO Auto-generated method stub
	}

	
	public boolean isQuickSeachAble() {	
		return true;
	}
	
	private void query(final Object afilter){
		 final ProgressDialog dialog = ProgressDialog.show(this, "", 
			        "查旬中，请稍等 …", true, true);
			new Thread(){
				
				public void run(){
					I_DataLogicERP dlERP=DataLogicPDA.getDLERP();
					try {
						List<Customer> lists=dlERP.listByFilterWithClassName(Customer.class.getName(), afilter);
						if(lists==null)
							lists=new ArrayList();
						mHandler.obtainMessage(MSG_SUCCESS, lists).sendToTarget();			
					} catch (BasicException e) {
						e.printStackTrace();
						mHandler.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败   
					}
					dialog.dismiss(); 
				}
				}.start();		
	}
	
	
	public void onQuickSeach(String sCode) {
		final QBFParameters paras=new QBFParameters();
		paras.or(new QBFParameter(I_Customer.CARD).equal(sCode))
		     .or(new QBFParameter(I_Customer.CITY).equal(sCode))
		     .or(new QBFParameter(I_Customer.POSTAL).equal(sCode))
		     .or(new QBFParameter(I_Customer.NAME1).link(sCode))
		     .or(new QBFParameter(I_Customer.COUNTRY).equal(sCode))
		     .or(new QBFParameter(I_Customer.CITY).equal(sCode))
		     .or(new QBFParameter(I_Customer.SEARCHKEY).link(sCode))
		     .or(new QBFParameter(I_Customer.REGION).equal(sCode)) 
		     .or(new QBFParameter(I_Customer.FAX).equal(sCode)) 
		     .or(new QBFParameter(I_Customer.PHONE).equal(sCode))
		     .or(new QBFParameter(I_Customer.PHONE2).equal(sCode))
		     .or(new QBFParameter(I_Customer.ADDRESS).equal(sCode));
		
		final ProgressDialog dialog = ProgressDialog.show(this, "", 
		        "查旬中，请稍等 …", true, true);
		new Thread(){
			
			public void run(){
				I_DataLogicERP dlERP=DataLogicPDA.getDLERP();
				try {
					List<Customer> lists=dlERP.queryByQBFParameters(Customer.class.getName(), paras);
					if(lists==null)
						lists=new ArrayList();
					mHandler.obtainMessage(MSG_SUCCESS, lists).sendToTarget();			
				} catch (BasicException e) {
					e.printStackTrace();
					mHandler.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败   
				}
				dialog.dismiss(); 
			}
			}.start();		
		
	}
	
	public Object createValue(){
		Object[] afilter = new Object[6];
		// Date range
		afilter[0] = QBFCompareEnum.COMP_NONE;		
		afilter[1] = null;
		
		afilter[2] =  QBFCompareEnum.COMP_NONE;		
		afilter[3] = null;

		//category
		if(customerCategory!=null){
			afilter[4] = QBFCompareEnum.COMP_EQUALS;
			afilter[5] = customerCategory.getID();
		}else{
		  afilter[4] = QBFCompareEnum.COMP_NONE;
		  afilter[5] = null;
		}
		
		return afilter;
	}

	@Override
	public AbstractBaseBeanListAdapter<Customer> getListAdapter() {
		return new CustomerListAdapter(this);
	}
 }
